package xyz.iguilty.workflow.example.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xyz.iguilty.workflow.core.service.WorkFlowActionService;
import xyz.iguilty.workflow.example.dao.BusinessTripRepository;
import xyz.iguilty.workflow.example.entity.BusinessTrip;
import xyz.iguilty.workflow.example.service.BusinessTripService;

import java.util.Date;

@Service(BusinessTrip.PROCESS_DEFINITION_KEY)
public class BusinessTripServiceImpl implements BusinessTripService {
    @Autowired
    private WorkFlowActionService workFlowService;
    @Autowired
    private BusinessTripRepository businessTripRepository;

    @Override
    public void saveBusinessForm(BusinessTrip businessTrip) {
        if (businessTrip.getCreateBy()==null){
            throw new IllegalArgumentException("请输入创建人姓名");
        }
        businessTrip.setCreateTime(new Date());
        businessTripRepository.save(businessTrip);
    }

    @Override
    public BusinessTrip getDetailById(String businessKey) {
        return businessTripRepository.findById(businessKey).orElse(null);
    }

    @Transactional
    @Override
    public void startProcess(BusinessTrip businessTrip) {
        businessTrip.addVariable("days",businessTrip.getDays());
        workFlowService.startProcessInstance(businessTrip);
        this.saveBusinessForm(businessTrip);
    }
}
